<template>
	<view class="content">
		<u-navbar 
		title="我的订单" 
		:bg-color="`${mainColor}`" 
		fixed placeholder autoBack
		:height="navigationBarHeight"
		:title-style="{color:`${textColor}`, fontSize:'16px', fontWeight:700}"
		:left-icon-color="`${textColor}`"
		v-if="showTitle"
		/>
		<view class="tab-box flex-ar" :style="{'top': `${navHeight}px`}">
			<view class="tab-item" v-for="(item, index) in tabs" :key="index" @click="selection = item">
				<view :class="{'active': selection.id == item.id}">
					{{item.name}}
					<view class="line"></view>
				</view>
			</view>
		</view>
		<view class="list" :style="{height: `calc(100vh - 41px - ${navHeight}px)`, marginTop: '41px'}" v-if="orders">
			<view class="empty-box" v-if="orders.length == 0">
				<u-empty mode="order">
				</u-empty>
			</view>
			<u-list v-else @scrolltolower="loadMore" :height="`calc(100vh - 41px - ${navHeight}px)`">
				<view style="height: 20px;"></view>
				<u-list-item v-for="(item, index) in orders" :key="index">
					<view class="order-item" @click="clickItem(item)">
						<view class="order-top flex-b">
							<view class="flex-ct" style="gap: 4px;">
								<image :src="baseUrl(item.card_icon)" class="card-icon"></image>
								<view>{{item.card_name}}</view>
								<div class="dot"></div>
								<view class="tag">{{item.card_category}}</view>
							</view>
							<view :class="{status: true, success: item.status == 1, error: item.status == 2}">
								{{getStatusText(item)}}
							</view>
						</view>
						<view class="order-info">
							<view class="flex-ct">
								<text class="label">订单号：</text>
								<text>{{item.order_no}}</text>
							</view>
							<view v-if="item.need_card_no" class="flex-ct">
								<text class="label">卡号：</text>
								<text>{{item.card_no}}</text>
							</view>
							<view v-else class="flex-ct">
								<text class="label">卡密：</text>
								<text>{{item.card_pw}}</text>
							</view>
						</view>
						<view class="order-bottom flex-b">
							<view class="">
								<text>面值：</text>
								<text>￥{{item.submit_face}}</text>
							</view>
							<view class="time">
								<text>{{item.create_datetime}}</text>
							</view>
						</view>
					</view>
				</u-list-item>
				<u-loadmore :status="status" icon line />
				<view style="height: 10px;"></view>
			</u-list>
		</view>
	</view>
</template>

<script>
	import {
		baseUrl,
		getOrders
	} from '@/api';
	import token from '@/utils/token';
	import {
		systemInfo
	} from '@/utils/mixin';

	export default {
		mixins: [systemInfo],
		data() {
			return {
				baseUrl,
				selection: {},
				tabs: [{
						id: 1,
						name: '全部',
						status: ''
					},
					{
						id: 2,
						name: '成功',
						status: 1
					},
					{
						id: 3,
						name: '失败',
						status: 2
					}
				],
				page: 1,
				orders: [],
				hasMore: false,
				status: 'loadmore'
			}
		},
		onLoad() {
			// #ifdef MP-ALIPAY
			uni.hideTabBar();
			// #endif
			this.selection = this.tabs[0];
			this.getSystemInfo();
		},
		onShow() {
			this.loadOrders();
		},
		onPullDownRefresh() {
			this.loadOrders();
		},
		methods: {
			async loadOrders() {
				if (!token.exists()) return;
				this.page = 1;
				const res = await getOrders(this.selection.status, this.page);
				this.orders = res.data;
				this.hasMore = res.is_next;
				this.status = res.is_next ? 'loadmore' : 'nomore';
				uni.stopPullDownRefresh();
			},
			async loadMore() {
				if (this.status != 'loading' && this.hasMore) {
					this.page += 1;
					this.status = 'loading';
					const res = await getOrders(this.selection.status, this.page);
					this.orders = this.orders.concat(res.data);
					this.hasMore = res.is_next;
					this.status = res.is_next ? 'loadmore' : 'nomore';
				}
			},
			getStatusText(item) {
				if (item.status == 0) return '审核中'
				if (item.status == 1) return '成功'
				if (item.status == 2) return '失败'
			},
			clickItem(item) {
				uni.navigateTo({
					url: `/pages/order/detail?id=${item.id}`
				})
			}
		},
		watch: {
			selection: async function(value, oldValue) {
				if (oldValue.id && value.id !== oldValue.id) {
					this.loadOrders();
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.tab-box {
		padding-top: 10px;
		height: 31px;
		background-color: #fff;
		color: #999;
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		z-index: 100;

		.tab-item {
			width: 25%;
			display: flex;
			justify-content: center;
		}

		.active {
			position: relative;
			font-weight: 500;
			color: $main-color;
			height: 100%;

			.line {
				position: absolute;
				width: 100%;
				height: 3px;
				bottom: 0px;
				left: 0;
				background-color: $main-color;
			}
		}
	}

	.list {
		.empty-box {
			display: flex;
			align-items: center;
			justify-content: center;
			height: 100%;
		}

		.order-item {
			margin: 0 10px 10px;
			border-radius: 6px;
			box-shadow: 0 0 5px 0 rgba(0, 0, 0, .1);

			.order-top {
				height: 41px;
				line-height: 41px;
				padding-left: 10px;
				font-size: 14px;
				border-bottom: 1px solid #f1f1f1;
				font-family: Times New Roman;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;

				.card-icon {
					width: 20px;
					height: 20px;
					border-radius: 50%;
				}

				.dot {
					width: 4px;
					height: 4px;
					background-color: #5ac725;
					border-radius: 4px;
					margin: 0 4px;
				}

				.tag {
					font-size: 12px;
					color: #5ac725;
				}

				.status {
					width: 52px;
					height: 26px;
					line-height: 26px;
					color: #fff;
					text-align: center;
					background-color: #f1a319;
					font-size: 14px;
					border-radius: 0 8px 0 8px;
				}

				.success {
					background-color: #3c9cff;
				}

				.error {
					background-color: #f56c6c;
				}
			}

			.order-bottom {
				height: 41px;
				line-height: 41px;
				padding: 0 10px;
				font-size: 14px;
				font-family: Times New Roman;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;

				.time {
					font-size: 12px;
					color: #666;
				}
			}

			.order-info {
				padding: 10px 0 10px 10px;
				border-bottom: 1px solid #f1f1f1;
				font-size: 14px;
				font-family: Times New Roman;

				.label {
					font-weight: bold;
					width: 160rpx;
					text-align: right;
				}
			}
		}

		.order-item:active {
			background-color: #eee;
		}

	}
</style>